{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
	<link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.preview.min.css' %}">
	<style>
        .panel-default {
            margin-top: 10px;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .panel-body {
            padding: 0;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }

        .function a {
            margin: 0 5px;
        }
	</style>
{% endblock %}

{% block content %}
	<div class="container-fluid">
		<div class="panel panel-default">
			<div class="panel-heading">
				<div>
					<i class="fa fa-book" aria-hidden="true"></i> wiki文档
				</div>
				<div class="function" id="menu">
					<a href="{% url 'wiki_add' project_id=request.tracer.project.id %}" class="btn btn-success btn-sm">
						<i class="fa fa-plus-circle" aria-hidden="true"> 新 建</i>
					</a>
					{% if wiki_obj %}
						<a href="{% url 'wiki_edit' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}"
						   class="btn btn-primary btn-sm" id="wiki_edit">
							<i class="fa fa-edit" aria-hidden="true"> 编 辑</i>
						</a>
						<a href="{% url 'wiki_delete' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}"
						   class="btn btn-danger btn-sm" id="wiki_delete">
							<i class="fa fa-trash" aria-hidden="true"> 删 除</i>
						</a>
					{% endif %}


				</div>
			</div>
			<div class="panel-body">
				<div class="col-sm-3 title-list">
					<ul id="catalog">

					</ul>
				</div>
				<div class="col-sm-9 content">
					{% if wiki_obj %}
						<div id="previewMarkdown">
							<textarea>{{ wiki_obj.content }}</textarea>
						</div>
					{% else %}
						<div style="text-align: center;margin-top: 50px">
							<h2>《{{ request.tracer.project.name }}》- wiki文档库</h2>
							<a href="{% url 'wiki_add' project_id=request.tracer.project.id %}">
								<i class="fa fa-plus-circle" aria-hidden="true"> 新建文章</i>
							</a>
						</div>
					{% endif %}


				</div>
			</div>
		</div>
	</div>

{% endblock %}

{% block js %}
	<script src="{% static 'plugin/editor-md/editormd.min.js' %}"></script>
	<script src="{% static 'plugin/editor-md/lib/marked.min.js' %}"></script>
	<script src="{% static 'plugin/editor-md/lib/prettify.min.js' %}"></script>
	<script src="{% static 'plugin/editor-md/lib/raphael.min.js' %}"></script>
	<script src="{% static 'plugin/editor-md/lib/underscore.min.js' %}"></script>
	<script src="{% static 'plugin/editor-md/lib/sequence-diagram.min.js' %}"></script>
	<script src="{% static 'plugin/editor-md/lib/flowchart.min.js' %}"></script>
	<script src="{% static 'plugin/editor-md/lib/jquery.flowchart.min.js' %}"></script>
	<script>

        // http://127.0.0.1:8000/mamage/2/wiki/detail/
        var WIKI_URL = "{% url 'wiki' project_id=request.tracer.project.id %}";

        $(function () {
            initCatalog();
            initPreviewMarkdown();
        });

        function initPreviewMarkdown() {
			editormd.markdownToHTML("previewMarkdown", {
                // 防止xss攻击
                htmlDecode: "style,script,iframe",
			});
        }

        function initCatalog() {
            $.ajax({
                url: '{% url "wiki_catalog" project_id=request.tracer.project.id %}',
                type: 'GET',
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        $.each(res.data, function (index, item) {
                            var href_detail = WIKI_URL + "?wiki_id=" + item.id;
                            let li = $("<li>").attr('id', "id_" + item.id).append($('<a>').text(item.title).attr('href', href_detail)).append($("<ul>"));

                            if (!item.parent_id) {
                                // 添加到catalog中
                                $("#catalog").append(li);
                            } else {
                                $("#id_" + item.parent_id).children('ul').append(li);
                            }
                        })
                    } else {
                        alert("初始化目录错误, 请刷新重试!")
                    }
                }
            })
        }
	</script>
{% endblock %}